﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRQIDT.functions
{
    public partial class Transform : UserControl
    {
        public Transform()
        {
            InitializeComponent();

            //listview1属性设置
            listView1.Columns.Add("序号", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段标示", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("执行时间", 100, HorizontalAlignment.Center);
        }
        int TPA_count = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            //初始化
            this.textBox1.Enabled = true;
            this.textBox1.Text = "";
            this.listView1.Clear();
            listView1.Columns.Add("序号", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段标示", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("任务段名称", 100, HorizontalAlignment.Center);
            listView1.Columns.Add("执行时间", 100, HorizontalAlignment.Center);

        }

        int TP_SN = 0;//序号
        //添加到任务序列
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int j = 0; j < Form1.TS_count; j++)
            {
                if (Form1.TSA[j].TS_id == comboBox1.SelectedItem.ToString())
                {
                    Form1.TSA[j].TS_isselected = true;
                    //添加到TransformProfile数组
                    Form1.TPA[TPA_count - 1].TS_id[TP_SN] = Form1.TSA[j].TS_id;
                    //添加到ListView
                    ListViewItem LVFM = new ListViewItem();
                    LVFM.Text = TP_SN.ToString();
                    LVFM.SubItems.Add(Form1.TSA[j].TS_id);
                    LVFM.SubItems.Add(Form1.TSA[j].TS_Name);
                    LVFM.SubItems.Add(Form1.TSA[j].Excuse_Time.ToString());
                    listView1.Items.Add(LVFM);
                    TP_SN++;//序号加1
                }
            }
        }
        //添加任务段
        private void AddTaskSegment_Click(object sender, EventArgs e)
        {                              
            if ((double.Parse(this.textBox1.Text) >= 1.0) || (double.Parse(this.textBox1.Text) <= 0.0))
                MessageBox.Show("请输入正确的概率！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                this.comboBox1.Enabled = true;
                this.comboBox1.Items.Clear();
                for (int i = 0; (i < Form1.TS_count) && (Form1.TSA[i].TS_isdeleted == false); i++)
                {
                    this.comboBox1.Items.Add(Form1.TSA[i].TS_id);
                }
                TPA_count++;//概率模型
                Form1.TPA[TPA_count - 1] = new Form1.TaskProfile();
                Form1.TPA[TPA_count - 1].Profile_probability = double.Parse(this.textBox1.Text);
            }
        }
        //删除任务段
        private void DeleteTaskSegment_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                int k = int.Parse(listView1.SelectedItems[0].SubItems[0].Text) + 1;
                if (k < listView1.Items.Count)
                {
                    for (int j = k; j < listView1.Items.Count; j++)
                    {
                        listView1.Items[j].SubItems[0].Text = (j - 1).ToString();
                    }
                }
                //修改选择标识
                for (int i = 0; i < Form1.TS_count; i++)
                {
                    if (Form1.TSA[i].TS_id == listView1.SelectedItems[0].SubItems[1].Text)
                    {
                        Form1.TSA[i].TS_isselected = false;
                    }
                }
                //正式删除
                listView1.SelectedItems[0].Remove();
            }
        }
    }
}
